/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence.dao;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Project;
import model.ScheduleBaseline;
import org.apache.commons.logging.Log;
import persistence.SysSql;
import persistence.interfaces.IScheduleBaselineDao;
import persistence.services.BaseDao;
import persistence.services.PersistenceException;
import utils.BaseModel;
import persistence.services.SmDatabase;
import utils.SmLogFactory;

/**
 *
 * @author jmoura
 */
public class ScheduleBaselineDao extends BaseDao implements IScheduleBaselineDao, Serializable {

    private static final Log log = SmLogFactory.getLog(ScheduleBaselineDao.class);

    @Override
    public List<ScheduleBaseline> list(Project project) throws PersistenceException {
        try {
            log.trace("Loading ScheduleBaselines:");
            PreparedStatement pstm = SmDatabase.prepareStatement(SysSql.SelectScheduleBaselineByProject);
            pstm.setInt(1, project.getId());
            List<BaseModel> loadList = super.list(pstm, "id_schedule_baseline");
            //convert back to the desired Class
            //TODO: there must be a better way to cast the entire List.
            List<ScheduleBaseline> retList = new ArrayList<>();
            for (BaseModel elem : loadList) {
                retList.add((ScheduleBaseline) elem);
            }
            return retList;
        } catch (SQLException ex) {
            throw new UnsupportedOperationException("Db Error", ex);
        }
    }

    @Override
    protected ScheduleBaseline loadColumns(ResultSet rs) throws SQLException, PersistenceException {
        ScheduleBaseline baseline;
        baseline = new ScheduleBaseline();
        baseline.setId(rs.getInt("id_project"));
        baseline.setName(rs.getString("nm_baseline"));
        baseline.setCreated(rs.getDate("dt_created"));
        baseline.setApproved(rs.getDate("dt_approved"));
        baseline.setVersion(rs.getInt("nu_version"));
        baseline.setComments(rs.getString("ds_change_comments"));
        return baseline;
    }

    @Override
    public void save(ScheduleBaseline baseline) throws PersistenceException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
